/**
 * Description:一些注释<br/>
 * CreateTime:2019-03-27<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
package cn.cnsy123.bookwebsiteserver.service.menu.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCountCallbackHandler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.cnsy123.bookwebsiteserver.entity.SysPageItemInfo;
import cn.cnsy123.bookwebsiteserver.repository.SysPageItemInfoDao;
import cn.cnsy123.bookwebsiteserver.service.menu.MenuInfoService;
import cn.cnsy123.bookwebsiteserver.vo.MenuResult;
import cn.cnsy123.bookwebsiteserver.vo.PageInfo;
import cn.cnsy123.bookwebsiteserver.vo.ztree.TreeResult;

/**
 * Description:一些注释<br/>
 * CreateTime:2019-03-27<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
@Service
public class MenuInfoServiceImpl implements MenuInfoService {
    
    private static final Logger logger = LoggerFactory.getLogger(MenuInfoServiceImpl.class);
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @Autowired
    private SysPageItemInfoDao sysPageItemInfoDao;
    
    @Override
    @Transactional(rollbackFor = RuntimeException.class,readOnly = true)
    public List<MenuResult> findMenuBarInfo(Long userId) {
        List<MenuResult> results = new ArrayList<MenuResult>();
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT a.group_id as groupId,IFNULL(a.group_name,'') as groupName,IFNULL(b.page_name,'') as pageName ,IFNULL(b.page_address,'') AS pageAddress FROM book_page_group_info a left join book_page_item_info b on a.group_id = b.group_id ORDER BY a.group_order ASC,b.page_order ASC");
        jdbcTemplate.query(sql.toString(), new Object[] {}, new RowCountCallbackHandler() {
            List<PageInfo> children = null;
            MenuResult menuResult = new MenuResult();
            String oldGroupId = "";
            int countItem = 0;
            @Override
            protected void processRow(ResultSet rs, int rowNum) throws SQLException {
                if(rowNum == 0) {
                    rs.last();
                    countItem = rs.getRow();
                    rs.beforeFirst();
                    rs.next();
                }
                if(StringUtils.isBlank(oldGroupId) || !oldGroupId.equals(rs.getString("groupId"))) {
                    if(StringUtils.isNotBlank(oldGroupId)) {
                        menuResult.setChildren(children);
                        results.add(menuResult);
                    }
                    children = new ArrayList<PageInfo>();
                    menuResult = new MenuResult();
                    menuResult.setGroupName(rs.getString("groupName"));
                    oldGroupId = rs.getString("groupId");
                    logger.info("菜单信息："+menuResult.getGroupName());
                }
                
                PageInfo pageInfo = new PageInfo();
                pageInfo.setPageName(rs.getString("pageName"));
                pageInfo.setPageAddress(rs.getString("pageAddress"));
                children.add(pageInfo);
                
                if((countItem-1) == rowNum) {
                    menuResult.setChildren(children);
                    results.add(menuResult);
                }
            }
            
        });
        return results;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class,readOnly = true)
    public List<TreeResult> findRolePrivMenuBarInfo() {
        List<TreeResult> results = new ArrayList<TreeResult>();
        StringBuffer sql = new StringBuffer();
        sql.append(" (SELECT  CONCAT('10',group_id) as id,group_name as `name` ,null as pId,false as checked,false as `open`,false as chkDisabled from book_page_group_info ORDER BY group_order ASC) UNION ALL (select page_id as id,page_name as `name` ,CONCAT('10',group_id) as pId,false as checked,false as `open`,false as chkDisabled from book_page_item_info ORDER BY page_order ASC) ");
        jdbcTemplate.query(sql.toString(), new Object[] {}, new RowCountCallbackHandler() {
            @Override
            protected void processRow(ResultSet rs, int rowNum) throws SQLException {
                TreeResult treeResult = new TreeResult();
                treeResult.setId(new Integer(rs.getInt("id")));
                treeResult.setName(rs.getString("name"));
                String pId = rs.getString("pId");
                treeResult.setpId(StringUtils.isNotBlank(pId) ? new Integer(pId) : null);
                treeResult.setChecked(rs.getInt("checked") == 0 ? false : true);
                treeResult.setOpen(rs.getInt("open") == 0 ? false : true);
                treeResult.setChkDisabled(rs.getInt("chkDisabled") == 0 ? false : true);
                treeResult.setNocheck(StringUtils.isNotBlank(pId) ? false : true);
                results.add(treeResult);
            }
            
        });
        return results;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class,readOnly = true)
    public SysPageItemInfo findPageInfoByUrl(String pageAddress) {
        return sysPageItemInfoDao.findByPageAddress(pageAddress);
    }

}
